import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:intl/intl.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/datas/health_about_info.dart';
import 'package:flutter_syt/domain/widgets/network_load_image.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/global_date_time_format.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

@RoutePage(name: "epidemicHealthListPage")
class EpidemicHealthList extends StatefulWidget {
  ///type  0 健康宣传  1 防疫信息   2健康知识库
  final int type;
  EpidemicHealthList({required this.type});
  @override
  EpidemicHealthListState createState() => EpidemicHealthListState();
}

class EpidemicHealthListState extends State<EpidemicHealthList> {
  var zhLang = true;
  var local =
      appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.type == 0
              ? S.current.Health_publicity
              : widget.type == 1
                  ? S.current.Prevention_information
                  : S.current.knowledge,
          style: FontStyles.headerTextStyle_18.copyWith(
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      body: NormalTableView<HealthAboutInfo>(
        padding: EdgeInsets.symmetric(horizontal: 15),
        itemBuilder: (ctx, item) {
          return GestureDetector(
            child:
                //  Column(
                //   crossAxisAlignment: CrossAxisAlignment.start,
                //   children: [
                //     // _buildKnowledgeCell(item).forEach((element) { })
                //   // widget.type == 2 ? for (var i = 0; i < listWidget.length; i++) listWidget[i],  : _buildListViewCell(item),

                //     Container(
                //       height: 0.5,
                //       color: Colors.black.withOpacity(0.1),
                //     )
                //   ],
                //   // child: ,
                // ),
                widget.type == 2
                    ? _buildKnowledgeCell(item)
                    : _buildListViewCell(item),
            behavior: HitTestBehavior.opaque,
            onTap: ClickUtils.debounce(() {
              RouteManager.push(context, item.path ?? '');
              // RouteManager.push(context, RoutMapping.feedbackDetail(item.id));
            }),
          );
        },
        getData: (page) async {
          if (widget.type == 0) {
            var json =
                await API.shared.epidemic.healthPropagandaList(page: page);
            return json;
          } else if (widget.type == 1) {
            var json = await API.shared.epidemic.healthMessageList(page: page);
            return json;
          } else {
            var json =
                await API.shared.epidemic.healthKnowledgeList(page: page);
            return json;
          }
        },
      ),
    );
  }

  Widget _buildKnowledgeCell(HealthAboutInfo healthModel) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(vertical: 15),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                child: Text(
                  healthModel.title ?? '',
                  // 'vadslkjvasldkjflasjdflkajsdlfjcaszxnvc,.mznxvklajdfvnmd,vlkmdslkfv',
                  maxLines: 2,
                  softWrap: false,
                  textAlign: TextAlign.left,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Color(0xff000000),
                    fontSize: 16,
                    fontWeight: FontWeight.w300,
                    fontFamily: 'PingFang SC',
                  ),
                ),
                // constraints: BoxConstraints(maxWidth: (1.sw - 124)),
              ),
              SizedBox(
                height: 16,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                // mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 4),
                    decoration: BoxDecoration(
                        color: Color(0xffFFF5E7),
                        borderRadius: BorderRadius.circular(4)),
                    height: 20,
                    child: Center(
                      child: Text(
                        healthModel.tag ?? '',
                        style:
                            TextStyle(color: Color(0xffE8692B), fontSize: 12),
                      ),
                    ),
                  ),
                  Text(
                    calculateYearMonthDay(healthModel.createTime ?? 0),
                    style: TextStyle(
                        color: Colors.black.withOpacity(0.4), fontSize: 11),
                  ),
                ],
              ),
            ],
          ),
        ),
        Container(
          height: 0.5,
          color: Colors.black.withOpacity(0.1),
        )
      ],
    );
    // return [
    //   SizedBox(
    //     height: 15,
    //   ),
    //   Text(
    //     healthModel.title ?? '',
    //     maxLines: 2,
    //     softWrap: false,
    //     textAlign: TextAlign.left,
    //     overflow: TextOverflow.ellipsis,
    //     style: TextStyle(
    //       color: Color(0xff000000),
    //       fontSize: 16,
    //       fontWeight: FontWeight.w300,
    //       fontFamily: 'PingFang SC',
    //     ),
    //   ),
    //   SizedBox(
    //     height: 16,
    //   ),
    //   Row(
    //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
    //     // mainAxisSize: MainAxisSize.max,
    //     children: [
    //       Container(
    //         padding: EdgeInsets.symmetric(horizontal: 4),
    //         decoration: BoxDecoration(
    //             color: Color(0xffFFF5E7),
    //             borderRadius: BorderRadius.circular(4)),
    //         height: 20,
    //         child: Center(
    //           child: Text(
    //             healthModel.tag ?? '',
    //             style: TextStyle(color: Color(0xffE8692B), fontSize: 12),
    //           ),
    //         ),
    //       ),
    //       Text(
    //         calculateYearMonthDay(healthModel.createTime ?? 0),
    //         style:
    //             TextStyle(color: Colors.black.withOpacity(0.4), fontSize: 11),
    //       ),
    //     ],
    //   ),
    //   SizedBox(
    //     height: 15,
    //   ),
    // ];
    // return Container(
    //   margin: EdgeInsets.symmetric(vertical: 15),
    //   child: Column(
    //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
    //     // mainAxisSize: MainAxisSize.max,
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: [
    //       Text(
    //         healthModel.title ?? '',
    //         maxLines: 2,
    //         softWrap: false,
    //         textAlign: TextAlign.left,
    //         overflow: TextOverflow.ellipsis,
    //         style: TextStyle(
    //           color: Color(0xff000000),
    //           fontSize: 16,
    //           fontWeight: FontWeight.w300,
    //           fontFamily: 'PingFang SC',
    //         ),
    //       ),
    //       Row(
    //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
    //         // mainAxisSize: MainAxisSize.max,
    //         children: [
    //           Container(
    //             padding: EdgeInsets.symmetric(horizontal: 4),
    //             decoration: BoxDecoration(
    //                 color: Color(0xffFFF5E7),
    //                 borderRadius: BorderRadius.circular(4)),
    //             height: 20,
    //             child: Center(
    //               child: Text(
    //                 healthModel.tag ?? '',
    //                 style: TextStyle(color: Color(0xffE8692B), fontSize: 12),
    //               ),
    //             ),
    //           ),
    //           Text(
    //             calculateYearMonthDay(healthModel.createTime ?? 0),
    //             style: TextStyle(
    //                 color: Colors.black.withOpacity(0.4), fontSize: 11),
    //           ),
    //         ],
    //       ),
    //     ],
    //   ),
    // );
  }

  Widget _buildListViewCell(HealthAboutInfo healthModel) {
    return Column(
      children: [
        Container(
            width: 1.sw,
            padding: EdgeInsets.symmetric(vertical: 15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(
                  width: (1.sw - 124),
                  height: 80,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    // mainAxisSize: MainAxisSize.max,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        child: Text(
                          healthModel.title ?? '',
                          maxLines: 2,
                          softWrap: false,
                          textAlign: TextAlign.left,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Color(0xff000000),
                            fontSize: 16,
                            fontWeight: FontWeight.w300,
                            fontFamily: 'PingFang SC',
                          ),
                        ),
                        // constraints: BoxConstraints(maxWidth: (1.sw - 124)),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        // mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 4),
                            decoration: BoxDecoration(
                                color: Color(0xffFFF5E7),
                                borderRadius: BorderRadius.circular(4)),
                            height: 20,
                            child: Center(
                              child: Text(
                                healthModel.tag ?? '',
                                style: TextStyle(
                                    color: Color(0xffE8692B), fontSize: 12),
                              ),
                            ),
                          ),
                          Text(
                            calculateYearMonthDay(healthModel.createTime ?? 0),
                            style: TextStyle(
                                color: Colors.black.withOpacity(0.4),
                                fontSize: 11),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                Container(
                  width: 80,
                  height: 80,
                  margin: EdgeInsets.only(left: 14),
                  // decoration: BoxDecoration(
                  //   borderRadius: BorderRadius.circular(8),
                  //   image: NetworkLoadImage.loadNetworkProviderImage(
                  //       healthModel.cover),
                  // ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: NetworkLoadImage.loadNetworkImage(
                      healthModel.cover ?? '',
                      fit: BoxFit.cover,
                    ),
                    // child: CachedNetworkImage(
                    //   imageUrl: healthModel.cover ?? '',
                    //   fit: BoxFit.cover,
                    //   progressIndicatorBuilder: (ctx, bu, pro) {
                    //     return Center(
                    //       child: CircularProgressIndicator(
                    //         strokeWidth: 1,
                    //         backgroundColor: Colors.white,
                    //         valueColor: AlwaysStoppedAnimation(Colors.blue),
                    //       ),
                    //     );
                    //   },
                    //   errorWidget: (context, url, error) {
                    //     return Container(
                    //       color: Colors.black26,
                    //     );
                    //   },
                    // ),
                  ),
                )
              ],
            )),
        Container(
          height: 0.5,
          color: Colors.black.withOpacity(0.1),
        )
      ],
    );
  }

  String calculateYearMonthDay(int createtime) {
    return zhLang == true ? formatDate(createtime) : enFormatDate(createtime);
    // DateTime? dateTime = DateTime.tryParse((createtime).toString());
    // return DateUtil.formatDate(dateTime, format: 'yyyy-MM-dd');
  }

  @override
  void initState() {
    super.initState();

    if (local.contains('zh')) {
      zhLang = true;
    } else {
      zhLang = false;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(EpidemicHealthList oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
